#!/bin/bash
# start server
# by yushaolong
# email: yusl@knowbox.cn
# 2018.06.16

if [ -f /tmp/census.pid ]; then
    echo "[error]: process is running, please check."
    exit
fi

curDir=$(cd `dirname $0`; pwd)

if [ "$1" != "dev" ] && [ "$1" != "test" ] && [ "$1" != "prod" ];then
    echo "[error]: need env [dev|test|prod]"
    echo "execute command like: [ bash start.sh dev ]"
    exit
fi

srcFile=${curDir}/conf/toml/"appconf_${1}.toml"
echo $srcFile
if [ ! -f $srcFile ]; then
    echo "[error]: toml config file not exist"
    echo "please check config file path [${srcFile}]"
    exit
fi


projectDir="${curDir}/../.."
export GOPATH=${GOPATH}:$projectDir

echo "start build..."
go build -o ${curDir}/census ${curDir}/bin/census.go
echo "build success."

echo "start server..."
nohup ${curDir}/census -conf=$srcFile 2>>/tmp/census.log.err 1>>/tmp/census.log.out &
echo "start server success."